// src/router/index.js
import {createRouter, createWebHistory} from 'vue-router'
import Home from '../views/user/Home.vue'
import UserLogin from "@/views/user/Login.vue";
import TradingPlatform from "@/views/user/TradingPlatform.vue";
import UserCenter from "@/views/user/UserCenter.vue";
import DefaultHome from "@/views/user/DefaultHome.vue";
import Register from "@/views/user/Register.vue";
import AdminLogin from "@/views/admin/Login.vue";
import {useUserStore} from "@/stores/index.js";
import AdminDashboard from "@/views/admin/AdminDashboard.vue";
import UserManagement from "@/components/admin/UserManagement.vue";
import ProductManagement from "@/components/admin/ProductManagement.vue";
import OrderManagement from "@/components/admin/OrderManagement.vue";
import Analytics from "@/components/admin/Analytics.vue";
import SystemSettings from "@/components/admin/SystemSettings.vue";
import Dashboard from "@/components/admin/Dashboard.vue";
// import {validateToken} from "@/utils/JwtUtils.js";
// import {message} from "ant-design-vue";

// 配置路由
const routes = [
    {
        path: '/', // 路由路径
        name: 'Home', // 路由名称
        component: Home, // 组件
        children: [
            {
                path: '',
                component: DefaultHome
            },
            {
                path: 'TradingPlatform',
                component: TradingPlatform
            },
            {
                path: 'UserCenter',
                component: UserCenter
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: UserLogin,
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
    },
    {
        path: '/adminLogin',
        name: 'AdminLogin',
        component: AdminLogin,
    },
    {
        path: '/admin',
        component: AdminDashboard,
        children: [
            {path: 'dashboard', name: 'dashboard', component: Dashboard},
            {path: 'users', name: 'users', component: UserManagement},
            {path: 'products', name: 'products', component: ProductManagement},
            {path: 'orders', name: 'orders', component: OrderManagement},
            {path: 'analytics', name: 'analytics', component: Analytics},
            {path: 'settings', name: 'settings', component: SystemSettings},
            {path: '', redirect: {name: 'dashboard'}}
        ]
    }
]

// 创建路由实例并传入路由配置
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore(); // 获取用户 store
    if (to.path === '/UserCenter' && !userStore.userInfo.userId) {
        next('/login'); // 如果没有 userId，跳转到登录页面
    } else {
        next();
    }

    // const token = localStorage.getItem('Authorization');
    // if (to.path === '/UserCenter' && !validateToken(token)) {
    //     // 如果token不存在或过期，跳转到登陆页面
    //     next('/login');
    //     message.error("身份验证过期");
    // } else {
    //     next();
    // }
});

export default router
